package com.wondertek.common.base;

import cn.hutool.http.HttpStatus;
import com.wondertek.common.util.base.MessageUtils;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serial;
import java.io.Serializable;

/**
 * @author xqs
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Result<T> implements Serializable {

    @Serial
    private static final long serialVersionUID = 1L;

    /**
     * 是否成功
     */
    @Builder.Default
    @Schema(name = "success", description = "true表示成功，false表示出错")
    private Boolean success = true;

    /**
     * 数据（返回单条数据，一般用于新增、修改等操作的返回值）
     */
    @Schema(name = "data", description = "成功时返回的数据")
    private T data;

    /**
     * 错误信息返回
     */
    @Builder.Default
    @Schema(name = "code", description = "返回编码")
    private int code = HttpStatus.HTTP_OK;

    /**
     * 错误信息返回
     */
    @Schema(name = "success", description = "提示信息,供报错时使用")
    private String message;

    /**
     * traceId 当前请求追踪id
     */
    private String traceId;

    /**
     * 时间戳
     */
    @Builder.Default
    @Schema(name = "timestamp", description = "时间戳")
    private Long timestamp = System.currentTimeMillis();

    /**
     * 成功返回
     *
     */
    public static Result<?> success() {
        return Result.builder()
                .message(MessageUtils.getMessage("result.success"))
                .build();
    }

    /**
     * 成功返回
     *
     */
    public static Result<?> success(String message) {
        return Result.builder()
                .message(MessageUtils.getMessage(message))
                .build();
    }

    /**
     * 成功返回
     *
     */
    @SuppressWarnings("all")
    public static <T> Result<T> success(T data) {
        return (Result<T>) Result.builder().data(data).message(MessageUtils.getMessage("result.success")).build();
    }


    /**
     * 成功返回
     *
     */
    @SuppressWarnings("all")
    public static <T> Result<T> success(T data, String message) {
        return (Result<T>) Result.builder()
                .data(data)
                .message(MessageUtils.getMessage(message))
                .build();
    }

    /**
     * 失败返回
     */
    public static Result<?> error(String msg) {
        return error(HttpStatus.HTTP_INTERNAL_ERROR,MessageUtils.getMessage(msg));
    }

    /**
     * 失败返回
     */
    public static Result<?> error(Integer code,String message) {
        return Result.builder()
                .code(code)
                .message(MessageUtils.getMessage(message))
                .success(false)
                .build();
    }

}
